package controlSoftware.DataIn;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;

import de.mud.telnet.*;

public class TelnetParser{
	public TelnetWrapper telnet = new TelnetWrapper();;
	private String host;
	private int port;
	public byte[] buffer = new byte[1024]; 
	public final char nlChar = '\n';
	public Queue<String> lineOfData = new LinkedList<String>();
	
	//host: 192.168.1.136
	//port: 80
	public TelnetParser(String host, int port){
		this.host = host;
		this.port = port;
	}
	
	public TelnetWrapper getTelnetConnection(){
		return telnet;
	}
	
	public void connect(){

		//sample code
		   try {
		     telnet.connect(host, port);
		   } catch(IOException e) {
		     e.printStackTrace();
		   }
	}
	
	public Queue<String> getReadings(byte[] buffer) throws IOException{
		ByteArrayInputStream si = null;

		try {
			telnet.read(buffer);
			si = new ByteArrayInputStream(buffer);
			int c;
			lineOfData.clear();
			
			while( (c = si.read()) !=  (int)nlChar ){
				lineOfData.add(Integer.toString(c));
			}	
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if( si != null ) si.close();
		}
		
		return lineOfData;		
	}
	
	public String parseByteStream(Queue<String> queue){
		//TODO
		return null;
	}
}